package com.shiguo365.market.display.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.recker.flybanner.FlyBanner;
import com.shiguo365.market.R;
import com.shiguo365.market.display.activity.DetailActivity;
import com.shiguo365.market.display.activity.MarketPriceActivity;
import com.shiguo365.market.display.adapter.ProductAdapter;
import com.shiguo365.market.bean.Product;
import com.shiguo365.market.utils.HandlerUtils;
import com.yalantis.phoenix.PullToRefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by caomenglong on 16/7/4.
 */
public class IndexFragment extends Fragment {

    private FlyBanner mBannerNet;//加载网络图片

    private List<Product> productList=new ArrayList<Product>();

    PullToRefreshView mPullToRefreshView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_index, container, false);
        ButterKnife.bind(this,view);

        initNetBanner(view); //加载轮播图

        initProduct();//初始化商品数据

        ProductAdapter adapter = new ProductAdapter(this.getActivity(),R.layout.item_product,productList);
        ListView listView1 =(ListView) view.findViewById(R.id.listView1);
        listView1.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView1);

        ListView listView2 =(ListView) view.findViewById(R.id.listView2);
        listView2.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView2);


        ListView listView3 =(ListView) view.findViewById(R.id.listView3);
        listView3.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView3);

        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Product product = productList.get(i);
                toast(product.getName()+Integer.toString(product.getId()));
                Intent intent =new Intent(getActivity(), DetailActivity.class);
                startActivity(intent);
            }
        });
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Product product = productList.get(i);
                toast(product.getName()+Integer.toString(product.getId()));
            }
        });
        listView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Product product = productList.get(i);
                toast(product.getName()+Integer.toString(product.getId()));
            }
        });



        //*********刷新功能



        mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        toast("刷新完成！");
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 2000);
            }
        });

        return view;
    }


    /**
     * 加载网页图片
     */
    private void initNetBanner(View view) {
        mBannerNet = (FlyBanner) view.findViewById(R.id.banner);

        String[] mImagesUrl = {
                "http://7xj380.com1.z0.glb.clouddn.com/2016-07-03_5778d0726ac00.png",
                "http://7xj380.com1.z0.glb.clouddn.com/2016-07-03_5778d43571e6e.png",
                "http://7xj380.com1.z0.glb.clouddn.com/2016-07-03_5778e8177ea01.png",
                "http://7xj380.com1.z0.glb.clouddn.com/2016-06-30_577479528ff61.png"
        };

        List<String> imgesUrl = new ArrayList<>();
        for (int i = 0; i < mImagesUrl.length; i++) {
            imgesUrl.add(mImagesUrl[i]);
        }
        mBannerNet.setImagesUrl(imgesUrl);

        mBannerNet.setOnItemClickListener(new FlyBanner.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                toast("点击了第" + position + "张图片");
            }
        });
    }

    private void toast(String str) {
        Toast.makeText(this.getActivity(), str, Toast.LENGTH_SHORT).show();
    }



    //初始化商品列表模拟
    private void initProduct(){
        Product product = new Product();
        product.setId(1);
        product.setName("海南菠萝蜜");
        product.setContext("海南菠萝蜜的介绍");
        product.setPrice(189.22);
        product.setSpecname("约23斤每箱");
        product.setImgurl("http://7xj380.com1.z0.glb.clouddn.com/2016-05-27_574828de4587e.png");
        productList.add(product);
        product = new Product();
        product.setId(2);
        product.setName("美国JJ级车厘子");
        product.setContext("美国JJ级车厘子的介绍");
        product.setPrice(500.00);
        product.setSpecname("约15斤每箱");
        product.setImgurl("http://7xj380.com1.z0.glb.clouddn.com/2016-07-01_57763965ecce4.jpg");
        productList.add(product);
        product = new Product();
        product.setId(3);
        product.setName("新疆金蜜哈密瓜");
        product.setContext("新疆金蜜哈密瓜的介绍");
        product.setPrice(100.00);
        product.setSpecname("约40斤每箱");
        product.setImgurl("http://7xj380.com1.z0.glb.clouddn.com/2016-07-05_577b2b5822232.jpg");
        productList.add(product);
        product = new Product();
        product.setId(4);
        product.setName("智利有籽红提");
        product.setContext("智利有籽红提的介绍");
        product.setPrice(100.00);
        product.setSpecname("约20斤每箱");
        product.setImgurl("http://7xj380.com1.z0.glb.clouddn.com/2016-05-29_574ab09d4c8f1.png");
        productList.add(product);


    }


    /**
     * 动态设置ListView的高度
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        if(listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }


    private OtherItemAction mMarketPriceAction = new OtherItemAction(MarketPriceActivity.class);

    private class OtherItemAction implements Runnable {

        private Class gotoClz;

        protected OtherItemAction(Class gotoClz) {
            this.gotoClz = gotoClz;
        }

        @Override
        public void run() {
            startActivity(new Intent(getActivity(), gotoClz));

        }

        public void startDelayed() {
            HandlerUtils.postDelayed(this, 100);
        }

    }

    @OnClick(R.id.tv_market_price)
    public void onTvMarketPriceOnClick(View view){
        mMarketPriceAction.startDelayed();
    }

}
